package com.selen.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.selen.dto.PageDTO;
import com.selen.dto.RoomInsertDTO;
import com.selen.dto.RoomPageDTO;
import com.selen.dto.RoomUpdateDTO;
import com.selen.entities.Room;
import com.selen.exception.ServiceException;
import com.selen.mapper.DeptMapper;
import com.selen.mapper.RoomMapper;
import com.selen.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Selen
 */
@Service
@CacheConfig(cacheNames = "room")
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private DeptMapper deptMapper;

    @CacheEvict(allEntries = true)
    @Override
    public int insert(RoomInsertDTO dto) {
        Room room = BeanUtil.copyProperties(dto, Room.class);
        room.setId(null);
        room.setDeleted(0);
        room.setCreated(LocalDateTime.now());
        room.setUpdated(LocalDateTime.now());
        int result = roomMapper.insert(room);
        if (result <= 0){
            throw new ServiceException("DB 插入失败");
        }
        return result;
    }

    @Cacheable(key = "#id", condition = "#id != null ", unless = "#result == null ")
    @Override
    public Room selectById(Long id) {
        Room result = roomMapper.selectById(id);
        if (ObjectUtil.isNull(result)){
            throw new ServiceException("ID不存在");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int updateById(RoomUpdateDTO dto) {
        Room room = BeanUtil.copyProperties(dto, Room.class);
        room.setCreated(null);
        room.setUpdated(LocalDateTime.now());
        int result = roomMapper.updateById(room);
        if (result <= 0){
            throw new ServiceException("DB 修改失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteById(Long id) {
        if (deptMapper.existByRoomId(id)){
            throw new ServiceException("该记录存在子记录，无法删除");
        }
        int result = roomMapper.logicDeleteById(id);
        if (result <= 0){
            throw new ServiceException("DB 逻辑删除失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteByIds(List<Long> ids) {
        if (deptMapper.existByRoomIds(ids)){
            throw new ServiceException("记录存在子记录，无法删除");
        }
        int result = roomMapper.logicDeleteByIds(ids);
        if (result <= 0){
            throw new ServiceException("DB 批量逻辑删除失败");
        }
        return result;
    }

    @Cacheable(key = "#root.methodName", unless = "#result == null ")
    @Override
    public List<Room> selectAll() {
        return roomMapper.selectAll();
    }

    @Cacheable(key = "#root.methodName + ':' + #dto.pageNum + ':' + #dto.pageSize ",
            unless = "#result == null ")
    @Override
    public PageInfo<Room> page(PageDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new PageInfo<>(roomMapper.selectAll());
    }

    @Cacheable(key = "#root.methodName + ':' + " +
    "#dto.getKeyword() + ':' + " +
    "#dto.getPageNum() + ':' + " +
    "#dto.getPageSize()", unless = "#result == null ")
    @Override
    public PageInfo<Room> dimSelect(RoomPageDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new PageInfo<>(roomMapper.selectByDimName(dto.getKeyword()));
    }


}
